package com.github.cosycode.flow.base;

/**
 * <b>Description : </b>
 * <p>
 * <b>created in </b> 2022/12/15
 * </p>
 *
 * @author pengfchen
 * @since
 **/
public interface IEnumStage<T extends JobFlowContext> {

    int getCode();

    void callAction(T context, ActionRecord<?> actionRecord) throws Exception;

    IEnumStage<T> toNextStage(T context, JobStageRecord<?> jobStageRecord);

    /**
     * @param context context
     * @return next stage, if the value is null, means the flow is end.
     */
    default IEnumStage<T> process(JobFlowContext context) {
        JobStageRecord<?> jobStageRecord = new JobStageRecord<>();
        jobStageRecord.setStage(this);
        try {
            callAction((T) context, jobStageRecord);
        } catch (Exception e) {
            // TODO 打印日志
            jobStageRecord.setException(e);
        }
        IEnumStage<T> tiEnumStage = toNextStage((T) context, jobStageRecord);
        context.addJobStageRecord(jobStageRecord);
        return tiEnumStage;
    }
}
